<?php
	//productos
	function get_products( $subcategoryID=false, $languageID=1, $filters=false ){
		if($subcategoryID) $select_sc = "AND sc.id = '$subcategoryID'";
		$query = "SELECT p.id, pl.name, pl.title, pl.subtitle, pl.description, pl.description_img, pl.pdf, p.img, scl.description as subcategory 
		FROM product as p, prod_lang as pl, subcategory as sc, subcat_lang as scl 
		WHERE
		 p.id = pl.product_id
		 AND p.subcategory_id = sc.id
		 AND scl.subcategory_id = sc.id
		 AND pl.language_id = '$languageID'
		 AND scl.language_id = '$languageID'
		 $select_sc
			";
		return do_query($query);
	}
	function get_product( $productID, $languageID=1 ){
		if($productID > 0){
			$product_where = "WHERE p.id = '$productID'
			AND pl.product_id = '$productID'";
			$product_limit = "";
		} else {
			$product_where = "WHERE p.id";
			$product_limit = "LIMIT 1";
		}
		
		
		$query = "SELECT p.id, pl.name, pl.title, pl.subtitle, pl.description, pl.description_img, pl.description2, pl.pdf, p.img, scl.description as subcategory, sc.category_id as category, p.subcategory_id, p.warranty_id
		FROM product as p, prod_lang as pl, subcategory as sc, subcat_lang as scl
		$product_where
		AND pl.language_id = '$languageID'
		AND sc.id = p.subcategory_id
		AND scl.subcategory_id = p.subcategory_id
		AND scl.language_id = '$languageID'
		$product_limit ";
		return do_query($query);
	}
	
	function remove_product($productID){
		//borro los archivos vinculados
		$query = "SELECT * FROM product WHERE id = '$productID' ";
		$result = do_query($query);
				
		if( file_exists(FILES_PATH.$result[0][img]) ) unlink(FILES_PATH.$result[0][img]);
		
		$query = "SELECT pl.description_img, pl.pdf
		FROM prod_lang as pl
		WHERE pl.product_id = '$productID'";
		$result = do_query($query);
		foreach ( $result as $pl ){
			if( file_exists(FILES_PATH.$pl[description_img]) ) unlink(FILES_PATH.$pl[description_img]);
			if( file_exists(FILES_PATH.$pl[pdf]) ) unlink(FILES_PATH.$pl[pdf]);
		}
		
		//borro de la tabla de productos
		$query = "DELETE FROM product WHERE id = '$productID';
		 ";
		do_query($query);
		
		$query = "DELETE FROM prod_lang WHERE product_id = '$productID' ";
		do_query($query);
	}
	
	//subcategorias
	function get_subcategories( $categoryID=false, $languageID=1 ){
		if( $categoryID ) $filter_category = "AND c.id = '$categoryID'";
		$query = "SELECT sc.id, sc.peso, scl.description, cl.description as category
		FROM subcategory as sc, subcat_lang as scl, category as c, cat_lang as cl
		WHERE sc.id = scl.subcategory_id
		AND scl.language_id = '$languageID' 
		AND c.id = sc.category_id
		AND c.id = cl.category_id
		$filter_category
		AND cl.language_id = '$languageID' 
		ORDER BY sc.peso";
		return do_query($query);
	}
	function get_subcategory( $subcategoryID ){
		$query = "SELECT *
		FROM subcategory as sc
		WHERE sc.id = '$subcategoryID'";
		return do_query($query);
	}
	
	//categorias
	function get_categories( $languageID=1 ){
		$query = "SELECT c.id, cl.description
		FROM category as c, cat_lang as cl
		WHERE c.id = cl.category_id
		AND cl.language_id = '$languageID' 
		ORDER BY c.peso";
		return do_query($query);
	}
	
	//garantias
	function get_warranties( $languageID=1 ){
		$query = "SELECT w.id, wl.description
		FROM warranty as w, war_lang as wl
		WHERE w.id = wl.warranty_id
		AND wl.language_id = '$languageID' ";
		return do_query($query);
	}
	function get_warranty( $warrantyID, $languageID=1 ){
		$query = "SELECT w.id, wl.description
		FROM warranty as w, war_lang as wl
		WHERE w.id = wl.warranty_id
		AND wl.language_id = '$languageID'
		AND w.id = '$warrantyID' ";
		return do_query($query);
	}
	
	//idiomas
	function get_languages(){
		$query = "SELECT *
		FROM language";
		return do_query($query);
	}
	
	//banners
	function get_banners($languageID=1){
		$query = "SELECT b.href, bl.img FROM banner as b, ban_lang as bl WHERE b.id = bl.banner_id AND bl.language_id = '$languageID' ";
		return do_query($query);
	}
	function updateBanner($bannerID, $langID){
		$name = 'banner'.$bannerID.'_'.$langID;
		if( $_FILES[$name]['tmp_name'] ){
		
			$extension = explode(".", $_FILES[$name]['name']);
			$extension = $extension[ count($extension) - 1];
			$pathImg = "$name.".$extension;
			move_uploaded_file($_FILES[$name]['tmp_name'], FILES_PATH.$pathImg);
					
			$query = "UPDATE ban_lang SET img = '$pathImg' WHERE banner_id = '$bannerID' AND language_id = '$langID' ";
			do_query($query);
			
		}
	}
	function updateBannerHREF($key, $bannerID){
		if( $_POST[$key]){
			$query = "UPDATE banner SET href = '$_POST[$key]' WHERE id = '$bannerID'";
			do_query($query);
		}
	}
?>